#ifndef __SOFT_I2C_DRIVER_H__
#define __SOFT_I2C_DRIVER_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "stm32f4xx_hal.h"
#include <stdbool.h>
#include <stdint.h>
#include "FreeRTOS.h"
#include "semphr.h"
#include "task.h"

// I2C操作结果定义
typedef enum {
    SOFT_I2C_OK = 0,
    SOFT_I2C_ERROR,
    SOFT_I2C_BUSY,
    SOFT_I2C_TIMEOUT,
    SOFT_I2C_INVALID_PARAM,
    SOFT_I2C_NACK,
    SOFT_I2C_ARBITRATION_LOST,
    SOFT_I2C_BUS_ERROR
} soft_i2c_status_t;

// I2C速度模式
typedef enum {
    SOFT_I2C_STANDARD_MODE = 0,    // 100kHz
    SOFT_I2C_FAST_MODE,           // 400kHz
    SOFT_I2C_FAST_MODE_PLUS,      // 1MHz
    SOFT_I2C_CUSTOM_MODE          // 自定义速度
} soft_i2c_speed_mode_t;

// I2C配置结构体
typedef struct {
    // GPIO配置 - 任意IO口
    GPIO_TypeDef *scl_port;        // SCL GPIO端口
    uint16_t scl_pin;              // SCL GPIO引脚
    GPIO_TypeDef *sda_port;        // SDA GPIO端口
    uint16_t sda_pin;              // SDA GPIO引脚
    
    // 时序配置
    soft_i2c_speed_mode_t speed_mode;  // 速度模式
    uint32_t clock_delay_us;           // 时钟延迟(us)，自定义速度时使用
    
    // DMA配置 (可选，用于大数据传输)
    DMA_Stream_TypeDef *dma_stream;    // DMA流
    uint32_t dma_channel;              // DMA通道
    bool use_dma;                      // 是否使用DMA
    
    // FreeRTOS配置
    uint32_t timeout_ms;               // 默认超时时间(ms)
} soft_i2c_config_t;

// I2C驱动句柄
typedef struct {
    soft_i2c_config_t config;          // 配置参数
    SemaphoreHandle_t bus_mutex;       // 总线互斥锁
    DMA_HandleTypeDef hdma;            // DMA句柄
    bool initialized;                  // 初始化标志
    bool bus_acquired;                 // 总线占用标志
    uint32_t clock_delay;              // 计算出的时钟延迟
} soft_i2c_handle_t;

// 函数声明
// I2C驱动接口
soft_i2c_status_t soft_i2c_init(soft_i2c_handle_t *handle);
soft_i2c_status_t soft_i2c_deinit(soft_i2c_handle_t *handle);

// 基础读写函数
soft_i2c_status_t soft_i2c_write(soft_i2c_handle_t *handle, uint8_t dev_addr, uint8_t *data, uint16_t len, uint32_t timeout);
soft_i2c_status_t soft_i2c_read(soft_i2c_handle_t *handle, uint8_t dev_addr, uint8_t *data, uint16_t len, uint32_t timeout);
soft_i2c_status_t soft_i2c_write_register(soft_i2c_handle_t *handle, uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint16_t len, uint32_t timeout);
soft_i2c_status_t soft_i2c_read_register(soft_i2c_handle_t *handle, uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint16_t len, uint32_t timeout);

// 单字节操作
soft_i2c_status_t soft_i2c_write_byte(soft_i2c_handle_t *handle, uint8_t dev_addr, uint8_t data, uint32_t timeout);
soft_i2c_status_t soft_i2c_read_byte(soft_i2c_handle_t *handle, uint8_t dev_addr, uint8_t *data, uint32_t timeout);
soft_i2c_status_t soft_i2c_write_reg_byte(soft_i2c_handle_t *handle, uint8_t dev_addr, uint8_t reg_addr, uint8_t data, uint32_t timeout);
soft_i2c_status_t soft_i2c_read_reg_byte(soft_i2c_handle_t *handle, uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint32_t timeout);

// 总线控制
soft_i2c_status_t soft_i2c_bus_acquire(soft_i2c_handle_t *handle, uint32_t timeout);
soft_i2c_status_t soft_i2c_bus_release(soft_i2c_handle_t *handle);
soft_i2c_status_t soft_i2c_is_device_ready(soft_i2c_handle_t *handle, uint8_t dev_addr, uint32_t timeout);

// 工具函数
soft_i2c_status_t soft_i2c_set_speed(soft_i2c_handle_t *handle, soft_i2c_speed_mode_t speed_mode, uint32_t custom_delay_us);
void soft_i2c_delay_us(uint32_t us);

// 获取默认配置
void soft_i2c_get_default_config(soft_i2c_config_t *config);

#ifdef __cplusplus
}
#endif

#endif /* __SOFT_I2C_DRIVER_H__ */
